package org.coder.consumer_sentinel_9005;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
public class ConsumerSentinel9005Application {

    public static void main(String[] args) {
//        initFlowQpsRule();
        SpringApplication.run(ConsumerSentinel9005Application.class, args);
    }

    private static void initFlowQpsRule() {
        //异常方式
        List<FlowRule> rules = new ArrayList<>();//流控规则列表
        FlowRule rule = new FlowRule("exception");//流控规则,test为资源名
        rule.setCount(2);//阈值
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);//QPS限流
        rule.setLimitApp("default");//默认，对所有的入口流量生效
        rules.add(rule);
        //bool方式
        FlowRule rulebool = new FlowRule("bool");//流控规则,test为资源名
        rulebool.setCount(2);//阈值
        rulebool.setGrade(RuleConstant.FLOW_GRADE_QPS);//QPS限流
        rulebool.setLimitApp("default");//默认，对所有的入口流量生效
        rules.add(rulebool);
        //@SentinelResource注解方式
        FlowRule rulesentinel = new FlowRule("sentinel");//流控规则
        rulesentinel.setCount(2);//阈值
        rulesentinel.setGrade(RuleConstant.FLOW_GRADE_QPS);//QPS限流
        rulesentinel.setLimitApp("default");//默认，对所有的入口流量生效
        rules.add(rulesentinel);

        //@SentinelResource注解方式，并发线程控制
        FlowRule thread = new FlowRule("thread");
        thread.setCount(1);//阈值
        thread.setGrade(RuleConstant.FLOW_GRADE_THREAD);//线程数限流
        thread.setLimitApp("default");//默认，对所有的入口流量生效
        rules.add(thread);

        FlowRuleManager.loadRules(rules);

        /*
            RuleConstant
                FLOW_GRADE_QPS  QPS限流
                FLOW_GRADE_THREAD  线程数限流
                FLOW_GRADE_RT  响应时间限流
                FLOW_GRADE_CONCURRENCY  并发数限流
        * */
    }
}
